package com.yanqu.road.server.manager.strongholdwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.rank.stronghold.CrossActivityUnionRank;
import com.yanqu.road.logic.bussiness.rank.CrossStrongholdActivityRankBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.cross.stronghold.CrossActivityUnionRankModel;
import com.yanqu.road.logic.cross.stronghold.CrossStrongholdActivityRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.peakstronghold.CrossPeakStrongholdActivityMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdRankMgr extends TempMgr {

    //activityId groupId
    private static Map<Integer, Map<Integer, CrossStrongholdGroupRankModel>> groupRankModelMap = new ConcurrentHashMap<>();

    public static CrossStrongholdGroupRankModel getGroupRankModel(int activityId, int groupId) {
        Map<Integer, CrossStrongholdGroupRankModel> modelMap = groupRankModelMap.get(activityId);
        if (null == modelMap) {
            synchronized (groupRankModelMap) {
                modelMap = groupRankModelMap.get(activityId);
                if (null == modelMap) {
                    modelMap = new ConcurrentHashMap<>();
                    groupRankModelMap.put(activityId, modelMap);
                }
            }
        }
        CrossStrongholdGroupRankModel rankModel = modelMap.get(groupId);
        if (null == rankModel) {
            synchronized (modelMap) {
                rankModel = modelMap.get(groupId);
                if (null == rankModel) {
                    rankModel = new CrossStrongholdGroupRankModel(activityId, groupId);
                    modelMap.put(groupId, rankModel);
                }
            }
        }

        return rankModel;
    }

    public static List<CrossStrongholdActivityRankListModel> getCrossActivityRankListModel(int activityId) {
        List<CrossStrongholdActivityRankListModel> modelList = new ArrayList<>();
        Map<Integer, CrossStrongholdGroupRankModel> rankModelMap = groupRankModelMap.get(activityId);
        if (null != rankModelMap) {
            for (CrossStrongholdGroupRankModel groupRankModel : rankModelMap.values()) {
                modelList.addAll(groupRankModel.getUserRankModelList());
            }
        }
        return modelList;
    }

    public static List<CrossActivityUnionRankModel> getCrossActivityUnionRankModelList(int activityId) {
        List<CrossActivityUnionRankModel> modelList = new ArrayList<>();
        Map<Integer, CrossStrongholdGroupRankModel> rankModelMap = groupRankModelMap.get(activityId);
        if (null != rankModelMap) {
            for (CrossStrongholdGroupRankModel groupRankModel : rankModelMap.values()) {
                modelList.addAll(groupRankModel.getUnionRankModelList());
            }
        }
        return modelList;
    }

    private void initCrossRankListModelMap() {
        synchronized (groupRankModelMap) {
            Map<Integer, ActivityInfo> activityInfoMap = CrossStrongholdActivityMgr.getOpenActivityInfoMap();
            List<Integer> activityIdList = new ArrayList<>();
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                activityIdList.add(activityInfo.getActivityId());
            }
            Map<Integer, Map<Integer, Map<Integer, LinkedList<CrossUserRank>>>> crossUserRankListMap = CrossStrongholdActivityRankBusiness.getCrossStrongholdRankListMap(activityIdList);
            for (Map.Entry<Integer, Map<Integer, Map<Integer, LinkedList<CrossUserRank>>>> activityIdEntry : crossUserRankListMap.entrySet()) {
                Integer activityId = activityIdEntry.getKey();
                for (Map.Entry<Integer, Map<Integer, LinkedList<CrossUserRank>>> groupEntry : activityIdEntry.getValue().entrySet()) {
                    int groupId = groupEntry.getKey();
                    CrossStrongholdGroupRankModel groupRankModel = getGroupRankModel(activityId, groupId);
                    for (Map.Entry<Integer, LinkedList<CrossUserRank>> conditionTypeEntry : groupEntry.getValue().entrySet()) {
                        int conditionType = conditionTypeEntry.getKey();
                        CrossStrongholdActivityRankListModel rankListModel = groupRankModel.getUserRankModel(conditionType, true);
                        rankListModel.setConditionType(conditionType);
                        int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossStrongholdActivityMgr.getActivityConditionInfoList(activityId));
                        if (maxRank <= 0) {
                            maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                        }
                        rankListModel.setRankNum(maxRank);
                        rankListModel.setRankList(conditionTypeEntry.getValue());
                    }
                }
            }

        }
    }

    private void initCrossUnionRankListModelMap() {
        synchronized (groupRankModelMap) {
            Map<Integer, ActivityInfo> activityInfoMap = CrossStrongholdActivityMgr.getOpenActivityInfoMap();
            List<Integer> activityIdList = new ArrayList<>();
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                activityIdList.add(activityInfo.getActivityId());
            }
            Map<Integer, Map<Integer, Map<Integer, LinkedList<CrossActivityUnionRank>>>> crossUnionRankListMap = CrossStrongholdActivityRankBusiness.getCrossActivityUnionRankListMap(activityIdList);
            for (Map.Entry<Integer, Map<Integer, Map<Integer, LinkedList<CrossActivityUnionRank>>>> activityIdEntry : crossUnionRankListMap.entrySet()) {
                Integer activityId = activityIdEntry.getKey();
                for (Map.Entry<Integer, Map<Integer, LinkedList<CrossActivityUnionRank>>> groupEntry : activityIdEntry.getValue().entrySet()) {
                    int groupId = groupEntry.getKey();
                    CrossStrongholdGroupRankModel groupRankModel = getGroupRankModel(activityId, groupId);
                    for (Map.Entry<Integer, LinkedList<CrossActivityUnionRank>> conditionTypeEntry : groupEntry.getValue().entrySet()) {
                        int conditionType = conditionTypeEntry.getKey();
                        CrossActivityUnionRankModel rankListModel = groupRankModel.getUnionRankModel(conditionType, true);
                        int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossStrongholdActivityMgr.getActivityConditionInfoList(activityId));
                        if (maxRank <= 0) {
                            maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                        }
                        rankListModel.setRankNum(maxRank);
                        rankListModel.setRankList(conditionTypeEntry.getValue());
                    }
                }
            }
        }
    }

    public static CrossStrongholdActivityRankListModel getCrossActivityRankListModel(int activityId, int groupId, int conditionType, boolean newIfNull) {
        CrossStrongholdGroupRankModel groupRankModel = getGroupRankModel(activityId, groupId);
        return groupRankModel.getUserRankModel(conditionType, newIfNull);
    }

    public static CrossActivityUnionRankModel getCrossActivityUnionRankListModel(int activityId, int groupId, int conditionType, boolean newIfNull) {
        CrossStrongholdGroupRankModel groupRankModel = getGroupRankModel(activityId, groupId);
        return groupRankModel.getUnionRankModel(conditionType, newIfNull);
    }

    public static void changeStrongholdUserRank(int activityId, CrossUserRank crossUserRank) {
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, crossUserRank.getUserId());
        CrossStrongholdActivityRankListModel rankListModel = getCrossActivityRankListModel(activityId, groupId, crossUserRank.getBigRankType(), true);
        if (null != rankListModel) {
            rankListModel.rankChange(crossUserRank);
        }
    }

    public static void changeStrongholdUnionRank(int activityId, int conditionType, CrossActivityUnionRank unionRank, boolean isUnionDel) {
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionRank.getUnionUid());
        CrossActivityUnionRankModel rankListModel = getCrossActivityUnionRankListModel(activityId, groupId, conditionType, true);
        if (null != rankListModel) {
            if (isUnionDel) {
                rankListModel.removeRank(unionRank.getUnionUid());
            } else {
                rankListModel.rankChange(unionRank);
            }
        }
    }

    public static int getCrossActivityRank(int activityId, int conditionType, long serverId, long userId) {
        int rank = -1;
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        CrossStrongholdActivityRankListModel rankListModel = getCrossActivityRankListModel(activityId, groupId, conditionType, false);
        if (null != rankListModel) {
            rank = rankListModel.getMyRank(userId, serverId);
        }
        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = CrossStrongholdActivityMgr.getOpenActivityInfo(activityId);
        if (null != activityInfo) {
            maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, CrossStrongholdActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
        }
        if (rank > maxRank) {
            rank = -1;
        }
        return rank;
    }

    public static int getCrossActivityUnionRank(int activityId, int conditionType, long serverId, String unionUid) {
        int rank = -1;
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, unionUid);
        CrossActivityUnionRankModel rankListModel = getCrossActivityUnionRankListModel(activityId, groupId, conditionType, false);
        if (null != rankListModel) {
            rank = rankListModel.getMyRank(unionUid);
        }
        /*int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = CrossStrongholdActivityMgr.getOpenActivityInfo(activityId);
        if (null != activityInfo) {
            maxRank = Math.max(ActivityHelper.getMaxRankByConditionType(conditionType, CrossStrongholdActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId())),10);

        }
        if (rank > maxRank) {
            rank = -1;
        }*/
        return rank;
    }

    /**
     * 获取商会前几名
     */
    @Deprecated
    public static List<String> getCrossActivityUnionRankTop(int activityId, int groupId, int conditionType, int topNum) {
        CrossActivityUnionRankModel rankListModel = getCrossActivityUnionRankListModel(activityId, groupId, conditionType, false);
        List<String> topList = new ArrayList<>();
        if (null != rankListModel) {
            List<CrossActivityUnionRank> list = rankListModel.getRankList();
            //看可以取到最多几个
            int rankNum = topNum <= list.size() ? topNum : list.size();
            for (int i = 0; i < rankNum; i++) {
                topList.add(list.get(i).getUnionUid());
            }
            return topList;
        }
        return topList;
    }


    @Override
    public boolean reloadData() throws Exception {
        groupRankModelMap = new ConcurrentHashMap<>();
        initCrossRankListModelMap();
        initCrossUnionRankListModelMap();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean save() {
        for (Map<Integer, CrossStrongholdGroupRankModel> modelMap : groupRankModelMap.values()) {
            for (CrossStrongholdGroupRankModel groupRankModel : modelMap.values()) {
                groupRankModel.save();
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
